﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
using System.Data.SqlClient;

public partial class adsales_Default : System.Web.UI.Page
{
    private string bigname;
    public int pagesize = 20;
    public string pager = "";
    string str = System.Configuration.ConfigurationSettings.AppSettings["ConnStr"];
    //int PageIndex
    //{
    //    set { this.ViewState["CurrentIndex"] = value; }
    //    get
    //    {
    //        int index = 0;
    //        object obj = this.ViewState["CurrentIndex"];
    //        if (obj != null)
    //        {
    //            index = (int)obj;
    //        }
    //        return index;
    //    }
    //}
    protected void Page_Load(object sender, EventArgs e)
    {
        btn_insert.Attributes.Add("OnClick", "chk()");
        //Button1.Attributes.Add("OnClick", "chk()");
        if (!IsPostBack)
        {
           
             //TUser1.CheckUser(this);
             Bind();
            //设定状态
             if ((this.Session["ecrm_email"] == null) || (this.Session["ecrm_email"].ToString() == ""))
             {

             }
             else
             {
                 string sid = this.Session["ecrm_uid"].ToString();
                 TUser oUser = new TUser(sid);
                 oUser.Select();
                 bigname = oUser.BigName;
             }
            //DbBind();
            //PageIndex = 0;
            //BindPageData();
            //this.LblCurrent.Text = "当前第 1 页 ";
            //this.BtnNext.Visible = false;
            //this.BtnPrev.Visible = false;
             DBddl();
        }
              
       if (Request["opration"] != null)
            switch (Request["opration"].ToString())
            {
                case "edit":
                    BindInput();
                    break;
                case "ddlbind":
                    DBddl();
                    break;
            }
    }

    public void Bind()
    {
        DBddl();
        string sSql = "";
        int page = 1;
        if (Request["page"] != null)
        {
            page = int.Parse(Request["page"].ToString());
            //sSql = "with a as ( select (select count(*) from t_ad_sales) as countnum, row_number() over(order by intime desc) as num,* from t_ad_sales ) select  * from a where num between " + ((page - 1) * pagesize + 1) + " and " + ((page - 1) * pagesize + pagesize);
            sSql = "with a as ( select (select count(*) from t_ad_sales) as countnum, row_number() over(order by intime desc) as num,* from t_ad_sales left join dbo.t_ad_setup_info2 on t_ad_setup_info2.infoid=t_ad_sales.jobposi and dbo.t_ad_setup_info2.isshow = 1 ) select  * from a where num between " + ((page - 1) * pagesize + 1) + " and " + ((page - 1) * pagesize + pagesize);
        }
        else
        {
            sSql = "with a as ( select (select count(*) from t_ad_sales) as countnum, row_number() over(order by intime desc) as num,* from t_ad_sales left join dbo.t_ad_setup_info2 on t_ad_setup_info2.infoid=t_ad_sales.jobposi and dbo.t_ad_setup_info2.isshow = 1 ) select * from a where num between 1 and " + pagesize;
        }
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        this.Repeater1.DataSource = myReader.FDataReader;
        this.Repeater1.DataBind();
        if (myReader.FDataReader.HasRows)
        {
            int countnum = TSqlDB.ExecuteScalar(sSql);
            if (countnum % pagesize > 0)
                pager = GenPagebar(page, countnum / pagesize + 1);
            else
                pager = GenPagebar(page, countnum / pagesize);
        }

    }

    private void BindInput()
    {
      
        if (Request["id"] != null)
        {
            TAdSales ta = new TAdSales(Request["id"].ToString());
            ta.Select();
            Response.Clear();
            Response.Write(ta.SalesCode + "," + ta.SalesName + "," + ta.Sex + "," + ta.Jobposi + "," + ta.Notice + "," + ta.Status+","+ta.Inman+","+ta.InTime.ToString());
            Response.Flush();
            Response.Close();
        }

    }
    private void DbBind()
    {
        string sSql = "select * from t_ad_sales left join dbo.t_ad_setup_info2 on t_ad_setup_info2.infoid=t_ad_sales.jobposi and dbo.t_ad_setup_info2.isshow = 1 order by intime desc";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        this.Repeater1.DataSource = myReader.FDataReader;
        this.Repeater1.DataBind();
       
        //关闭
        myReader.Free();
    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string id = e.CommandArgument as string;

        //if (e.CommandName == "Del")
        //{
            
        //}
        switch (e.CommandName)
        {
            case "Del":
                TData.DeleteByID(id, "t_ad_sales", "SalesCode");
            Bind();
            break;

            case "update":

               break;
        }
       
    }
    protected void btn_insert_Click(object sender, EventArgs e)
    {
        try
        {
            string sql="";
            if (this.cbox_state.Checked == true)
            {
                sql = "insert into dbo.t_ad_sales(Salescode,Salesname,Sex,jobposi,notice,Status,Inman) values('" + this.txt_id.Text.Trim() + "','" + this.txt_name.Text.Trim() + "','" + this.ddl_sex.Text.Trim() + "','" + this.ddl_post.SelectedValue + "','" + this.txt_text.Text.Trim() + "',0,'" + Session["userid"].ToString() + "')";
            }
            else
            {
                sql = "insert into dbo.t_ad_sales(Salescode,Salesname,Sex,jobposi,notice,Status,Inman) values('" + this.txt_id.Text.Trim() + "','" + this.txt_name.Text.Trim() + "','" + this.ddl_sex.Text + "','" + this.ddl_post.SelectedValue + "','" + this.txt_text.Text.Trim() + "',1,'" + Session["userid"].ToString() + "')";
            }
            //string sql = "insert into dbo.t_ad_sales(Salescode,Salesname,Sex,jobposi,notice,Status,Inman) values('" + this.txt_id.Text + "','" + this.txt_name.Text + "','" + this.ddl_sex.Text + "','" + this.txt_post.Text + "','" + this.txt_text.Text + "','" + this.ddl_state.Text + "','" + this.txt_insertname.Text + "')";
            TSqlDB tsql = new TSqlDB();
            tsql.Execute(sql);
            this.Response.Write("<script>");
            this.Response.Write("alert('添加成功！')");
            this.Response.Write("</script>");
            this.txt_id.Text = "";
            //this.txt_insertname.Text = "";
            this.txt_name.Text = "";

            this.txt_text.Text = "";
        }
        catch(Exception ex)
        {
            this.Response.Write("<script>alert('编号以存在，请重新编写！')</script>");
            this.Response.Write(ex.Message.ToString());
        }
        Bind();
    }

    //protected void btn_update_Click(object sender, EventArgs e)
    //{
        
    //}

    protected void Button1_Click(object sender, EventArgs e)
    {
        this.txt_utext.Attributes.Add("OnClick", "kk(this)");   

        string sql = "";
        this.lbl_id.Text = this.HiddenField1.Value;
        if (this.cbox_ustate.Checked == true)
        {
            sql = "update t_ad_sales set Salesname ='" + this.txt_uname.Text + "', Sex ='" + this.ddl_usex.Text + "', Jobposi='" + ddl_upost.SelectedValue + "', Notice ='" + this.txt_utext.Text + "', Status =0 where SalesCode='" + lbl_id.Text + "'";
        }
        else
        { 
             sql = "update t_ad_sales set Salesname ='" + this.txt_uname.Text + "', Sex ='" + this.ddl_usex.Text + "', Jobposi='" + ddl_upost.SelectedValue + "', Notice ='" + this.txt_utext.Text + "', Status =1 where SalesCode='" + lbl_id.Text + "'";
        }
        
        
        TSqlDB tsql = new TSqlDB();
        tsql.Execute(sql);
        this.Response.Write("<script type='text/javascript'>alert('修改成功！')</script>");
        
        this.txt_uname.Text = "";
       
        this.txt_utext.Text = "";
        Bind();
    }

    protected void btn_look_Click(object sender, EventArgs e)
    {
        //select t_ad_sales.salescode,t_ad_sales.salesname,t_ad_sales.sex,t_ad_sales.jobposi,t_ad_sales.notice,t_ad_sales.status,t_ad_sales.inman from t_ad_sales left join dbo.t_ad_setup_info2 on t_ad_setup_info2.infoid=t_ad_sales.jobposi and dbo.t_ad_setup_info2.isshow = 1 where salescode like '%0%'union select t_ad_sales.salescode,t_ad_sales.salesname,t_ad_sales.sex,t_ad_sales.jobposi,t_ad_sales.notice,t_ad_sales.status,t_ad_sales.inman  from t_ad_sales where salesname like'% %'
        if (this.txt_SelectId.Text.Trim() == "" && this.txt_SelectName.Text.Trim() == "")
        {
            Bind();
        }
        else
        { 
            string sql = "select * from t_ad_sales where salescode like '%" + this.txt_SelectId.Text + "%' and salesname like'%" + this.txt_SelectName.Text + "%'";
            if (this.txt_SelectName.Text == "")
            {
                this.txt_SelectName.Text = " ";
            }
            if (this.txt_SelectId.Text == "")
            {
                this.txt_SelectId.Text = " ";
            }
            //sql = "select t_ad_sales.salescode,t_ad_sales.salesname,t_ad_sales.sex,t_ad_sales.jobposi,t_ad_sales.notice,t_ad_sales.status,t_ad_sales.inman from t_ad_sales left join dbo.t_ad_setup_info2 on t_ad_setup_info2.infoid=t_ad_sales.jobposi and dbo.t_ad_setup_info2.isshow = 1 where salescode like '%" + this.txt_SelectId.Text + "%'union select t_ad_sales.salescode,t_ad_sales.salesname,t_ad_sales.sex,t_ad_sales.jobposi,t_ad_sales.notice,t_ad_sales.status,t_ad_sales.inman  from t_ad_sales where salesname like'% " + this.txt_SelectName.Text + "%'";
            sql = "select * from t_ad_sales left join dbo.t_ad_setup_info2 on t_ad_setup_info2.infoid=t_ad_sales.jobposi and dbo.t_ad_setup_info2.isshow = 1 where salescode like '%"+this.txt_SelectId.Text.Trim()+"%' and salesname like'%"+this.txt_SelectName.Text.Trim()+"%' union select * from t_ad_sales left join dbo.t_ad_setup_info2 on t_ad_setup_info2.infoid=t_ad_sales.jobposi and dbo.t_ad_setup_info2.isshow = 1 where salescode like '%"+this.txt_SelectId.Text.Trim()+"%' and salesname like'%"+this.txt_SelectName.Text.Trim() +"%'";
            TSqlDBReader myReader = new TSqlDBReader(sql);
            this.Repeater1.DataSource = myReader.FDataReader;
            this.Repeater1.DataBind();
            this.txt_SelectId.Text = "";
            this.txt_SelectName.Text = "";
            //BindPageData();
            //Bind();
            getpager();
        }
        

    }

    public void getpager()
    {
        string sSql = "";
        int page = 1;
        if (Request["page"] != null)
        {
            page = int.Parse(Request["page"].ToString());
            sSql = "with a as ( select (select count(*) from t_ad_sales) as countnum, row_number() over(order by intime desc) as num,* from t_ad_sales ) select  * from a where num between " + ((page - 1) * pagesize + 1) + " and " + ((page - 1) * pagesize + pagesize);
        }
        else
        {
            sSql = "with a as ( select (select count(*) from t_ad_sales) as countnum, row_number() over(order by intime desc) as num,* from t_ad_sales ) select * from a where num between 1 and " + pagesize;
        }
        TSqlDBReader myReader = new TSqlDBReader(sSql);
       
        if (myReader.FDataReader.HasRows)
        {
            int countnum = TSqlDB.ExecuteScalar(sSql);
            if (countnum % pagesize > 0)
                pager = GenPagebar(page, countnum / pagesize + 1);
            else
                pager = GenPagebar(page, countnum / pagesize);
        }

    }
    protected void btn_full_Click(object sender, EventArgs e)
    {
        Bind();
    }
   

    private void BindPageData()
    {
        //string sql = "select * from t_ad_sales where 1=1 ";
        //if (this.txt_SelectId.Text != "")
        //    sql += " and salescode like '%" + txt_SelectId.Text + "%' ";
        //if(txt_SelectName.Text !="")
        //    sql += " and salesname like '%" + txt_SelectName.Text + "%' ";
        //string str = System.Configuration.ConfigurationSettings.AppSettings["ConnStr"];
        //SqlConnection con = new SqlConnection(str);
        //SqlDataAdapter da = new SqlDataAdapter(sql, con);
        //DataSet ds = new DataSet();
        //da.Fill(ds);
        //this.DataList1.DataSource = ds;
        //this.DataList1.DataBind();
        //int PageSize = 20;
        //int startIndex = PageSize * PageIndex;
        //int endIndex = Math.Min(startIndex + PageSize, ds.Tables[0].DefaultView.Count);
        //System.Collections.ArrayList list = new System.Collections.ArrayList();
        //for (int i = startIndex; i < endIndex; i++)
        //{
        //    list.Add(ds.Tables[0].DefaultView[i]);
        //}

        //this.Repeater1.DataSource = list;
        //this.Repeater1.DataBind();

        //this.LblCurrent.Text = string.Format("当前第 {0} 页", this.PageIndex + 1);
        //this.BtnPrev.Visible = (this.PageIndex != 0);
        //int PageCount = (ds.Tables[0].DefaultView.Count + PageSize - 1) / PageSize;

        //this.BtnNext.Visible = (this.PageIndex + 1 != PageCount);

        //int[] array = new int[PageCount];
        //for (int i = 1; i < PageCount; i++)
        //{
        //    array[i] = i;
        //}

        //this.DataList1.DataSource = array;
        //this.DataList1.SelectedIndex = this.PageIndex;
        //this.DataList1.RepeatColumns = PageCount;
        //this.DataList1.DataBind();
        //if (list.Count == 0)
        //{
        //    this.BtnNext.Visible = false;
        //    this.BtnPrev.Visible = false;
        //}
        //this.lab_full.Text = list.Count.ToString();
    }
    protected void BtnPrev_Click(object sender, EventArgs e)
    {
        //this.PageIndex--;
        //this.BindPageData();
    }
    protected void BtnNext_Click(object sender, EventArgs e)
    {
        //this.PageIndex++;
        //this.BindPageData();
    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        //if (e.CommandName == "Pager")
        //{
        //    int index = Convert.ToInt32(e.CommandArgument);
        //    this.PageIndex = index;
        //    this.BindPageData();
        //}
    }
    protected void btnPanel2Cancel_Click(object sender, EventArgs e)
    {

    }

    public string GenPagebar(int PageNum, int PageCounts)
    {
        string result = "<div class=\"pagebox\">";


        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='Default.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a href='Default.aspx?opration=page&page=2'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='Default.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a href='Default.aspx?opration=page&page=1'\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='Default.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='Default.aspx?opration=page&page=" + (PageNum - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a href='Default.aspx?opration=page&page=" + (PageNum + 1).ToString() + "'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='Default.aspx?opration=page&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a href='Default.aspx?opration=page&page=1'\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='Default.aspx?opration=page&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='Default.aspx?opration=page&page=" + (PageCounts - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + pagesize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }
    private void DBddl()
    {
        string sql = "select infoid,infoname from t_ad_setup_info2 where typeid = 'S01'";
        TSqlDBReader myReader = new TSqlDBReader(sql);
        
        ddl_post.DataValueField = "infoid";
        ddl_post.DataTextField = "infoname";
        ddl_post.DataSource = myReader.FDataReader;
        ddl_post.DataBind();

        TSqlDBReader myReader2 = new TSqlDBReader(sql);
        ddl_upost.DataValueField = "infoid";
        ddl_upost.DataTextField = "infoname";
        ddl_upost.DataSource = myReader2.FDataReader;
        ddl_upost.DataBind();
    }


    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Bind();
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Bind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Bind();
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        Bind();
    }
}
